package cn.edu.bzu.manager.computer.controller;

import cn.edu.bzu.manager.computer.entity.DeliverInfo;
import cn.edu.bzu.manager.computer.entity.TypeInfo;
import cn.edu.bzu.manager.computer.service.AllComponentService;
import cn.edu.bzu.manager.computer.service.DeliverService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单信息控制器
 *
 * @author 1923111015张铭宸
 * @since 1.8
 */

@RestController
@AllArgsConstructor
@RequestMapping("/deliver")
public class DeliverController {

    private final DeliverService deliverService;
    private final AllComponentService allComponentService;

    /**
     * GET
     *
     * @return 所有订单信息JSON数组
     */
    @GetMapping("/all")
    public List<DeliverInfo> showAllDeliverInfos() {
        List<DeliverInfo> infos = deliverService.findAllDeliverInfos();
        for (DeliverInfo info : infos) {
            info.setComponent(allComponentService.
                    findComponentInfoByTypeIdAndComponentId(info.getTypeId(), info.getComponentId()));
        }
        return infos;
    }

    /**
     * GET
     *
     * @return 所有硬件类型信息JSON数组
     */
    @GetMapping("/type/all")
    public List<TypeInfo> showAllTypeInfos() {
        return deliverService.findAllTypeInfos();
    }

    /**
     * POST
     *
     * @param deliverInfo 要添加的订单JSON对象信息
     */
    @PostMapping("/")
    public void addDeliver(@RequestBody DeliverInfo deliverInfo) {
        deliverService.saveDeliverInfo(deliverInfo);
    }

}
